C
C     ..................................................................
C
C        SUBROUTINE PERM
C
C        PURPOSE
C           TO COMPUTE THE PERMUTATION VECTOR THAT IS INVERSE TO A GIVEN
C           PERMUTATION VECTOR, THE PERMUTATION VECTOR THAT IS EQUIVA-
C           LENT TO A GIVEN TRANSPOSITION VECTOR AND A TRANSPOSITION
C           VECTOR THAT IS EQUIVALENT TO A GIVEN PERMUTATION VECTOR.
C           (SEE THE GENERAL DISCUSSION FOR DEFINITIONS AND NOTATION.)
C
C        USAGE
C           CALL PERM(IP1,IP2,N,IPAR,IER)
C
C        DESCRIPTION OF PARAMETERS
C           IP1  - GIVEN PERMUTATION OR TRANSPOSITION VECTOR
C                  (DIMENSION N)
C           IP2  - RESULTING PERMUTATION OR TRANSPOSITION VECTOR
C                  (DIMENSION N)
C           N    - DIMENSION OF VECTORS IP1 AND IP2
C           IPAR - INPUT PARAMETER
C                  IPAR NEGATIVE - COMPUTE THE PERMUTATION VECTOR IP2
C                                  THAT IS THE INVERSE OF THE PERMUTA-
C                                  TION VECTOR IP1
C                  IPAR  =  ZERO - COMPUTE THE PERMUTATION VECTOR IP2
C                                  THAT IS EQUIVALENT TO THE TRANSPOSI-
C                                  TION VECTOR IP1
C                  IPAR POSITIVE - COMPUTE A TRANSPOSITION VECTOR IP2
C                                  THAT IS EQUIVALENT TO THE PERMUTATION
C                                  VECTOR IP1
C           IER  - RESULTING ERROR PARAMETER
C                  IER=-1  -  N IS NOT POSITIVE
C                  IER= 0  -  NO ERROR
C                  IER= 1  -  IP1 IS EITHER NOT A PERMUTATION VECTOR OR
C                             NOT A TRANSPOSITION VECTOR ON 1,...,N,
C                             DEPENDING ON WHETHER IPAR IS NON-ZERO OR
C                             ZERO, RESPECTIVELY
C
C        REMARKS
C           (1)  IF IER=-1 THERE HAS BEEN NO COMPUTATION.
C           (2)  IF IER=1, THEN COMPUTATION HAS BEEN UNSUCCESSFUL DUE TO
C                ERROR AND THE PARTIAL RESULTS FOUND IN IP2 ARE USELESS.
C           (3)  IP2 CANNOT HAVE THE SAME STORAGE ALLOCATION AS IP1.
C
C        SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED
C           NONE
C
C        METHOD
C           (1)  IPAR NEGATIVE - FOR EACH I, I=1,...,N, IP2(IP1(I)) IS
C                                SET TO I.
C           (2)  IPAR  =  ZERO - INITIALLY IP2(I) IS SET TO I FOR
C                                I=1,...,N.  THEN, FOR I=1,...,N IN THAT
C                                ORDER, IP2(I) AND IP2(IP1(I)) ARE
C                                INTERCHANGED.
C           (3)  IPAR POSITIVE - INITIALLY IP1 IS MOVED TO IP2.  THEN
C                                THE FOLLOWING TWO STEPS ARE REPEATED
C                                FOR I SUCCESSIVELY EQUAL TO 1,...,N.
C                                (A) FIND THE SMALLEST J GREATER THAN OR
C                                    EQUAL TO I SUCH THAT IP2(J)=I.
C                                (B) SET IP2(J) TO IP2(I).
C
C     ..................................................................
C
      SUBROUTINE PERM(IP1,IP2,N,IPAR,IER)
C
C
      DIMENSION IP1(1),IP2(1)
C
C        TEST DIMENSION
      IF(N)19,19,1
C
C        TEST IPAR TO DETERMINE WHETHER IP1 IS TO BE INTERPRETED AS
C        A PERMUTATION VECTOR OR AS A TRANSPOSITION VECTOR
    1 IF(IPAR)2,13,2
C
C        CHECK THAT IP1 IS A PERMUTATION VECTOR AND COMPUTE IP1 INVERSE
    2 DO 3 I=1,N
    3 IP2(I)=0
      DO 6 I=1,N
      K=IP1(I)
      IF(K-N)4,5,20
    4 IF(K)20,20,5
    5 IF(IP2(K))20,6,20
    6 IP2(K)=I
C
C        TEST IPAR FOR THE DESIRED OPERATION
      IF(IPAR)12,7,7
C
C        COMPUTE TRANSPOSITION VECTOR IP2 FOR PERMUTATION VECTOR IP1
    7 DO 8 I=1,N
    8 IP2(I)=IP1(I)
      NN=N-1
      IF(NN)12,12,9
    9 DO 11 I=1,NN
      DO 10 J=1,NN
      IF(IP2(J)-I)10,11,10
   10 CONTINUE
      J=N
   11 IP2(J)=IP2(I)
C
C        NORMAL RETURN - NO ERROR
   12 IER=0
      RETURN
C
C        COMPUTE PERMUTATION VECTOR IP2 FOR TRANSPOSITION VECTOR IP1
   13 DO 14 I=1,N
   14 IP2(I)=I
      DO 18 I=1,N
      K=IP1(I)
      IF(K-I)15,18,16
   15 IF(K)20,20,17
   16 IF(N-K)20,17,17
   17 J=IP2(I)
      IP2(I)=IP2(K)
      IP2(K)=J
   18 CONTINUE
      GO TO 12
C
C        ERROR RETURN - N IS NOT POSITIVE
   19 IER=-1
      RETURN
C
C        ERROR RETURN - IP1 IS EITHER NOT A PERMUTATION VECTOR
C                       OR NOT A TRANSPOSITION VECTOR
   20 IER=1
      RETURN
      END
